package shoppingmall.manage.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import shoppingmall.manage.dto.CommodityVO;
import shoppingmall.manage.mapper.CommodityCategoryMapper;
import shoppingmall.manage.mapper.CommodityMapper;
import shoppingmall.manage.mapper.CommodityStandardMapper;
import shoppingmall.manage.pojo.Commodity;
import shoppingmall.manage.pojo.CommodityCategory;
import shoppingmall.manage.pojo.CommodityStandard;
import shoppingmall.manage.dto.PageBean;
import shoppingmall.manage.service.CommodityService;

import java.util.ArrayList;
import java.util.List;

@SuppressWarnings({"all"})
@Service
public class CommodityServiceImpl implements CommodityService {

    @Autowired
    private CommodityMapper commodityMapper;

    /* 查询商品信息时需要对商品分类表、商品规格表进行查询 */
    @Autowired
    private CommodityCategoryMapper commodityCategoryMapper;
    @Autowired
    private CommodityStandardMapper commodityStandardMapper;

    /**
     * 分页查询商品列表数据
     */
    @Override
    public PageBean getCommodityList(int page, int pageSize) {
        //查询商品的总条数
        int commodityCount = commodityMapper.getCommodityCount();
        //分页查询商品列表
        int start = (page - 1) * pageSize;
        List<Commodity> commodityList = commodityMapper.getCommodityList(start, pageSize);

        /**此处要将商品列表的某些字段进行替换,并添加商品规格字段数据commodity_category_id --> categoryName(商品名称) 添加商品规格字段List<String> commodity_standard */
        //1.通过commodityList中字段commodity_category_id => commodityCategoryName
        //2.通过commodity_id 遍历商品规格表，将所有的规格信息封装在List<String>集合中
        List<CommodityCategory> allCommodityCategoryList = commodityCategoryMapper.getAllCommodityCategoryList();  //商品分类表所有数据
        List<CommodityStandard> allCommodityStandardList = commodityStandardMapper.getAllCommodityStandardList();  //商品规格表所有数据
        List<CommodityVO> commodityVOList = new ArrayList<>();
        for(Commodity commodity : commodityList) {
            CommodityVO commodityVO = new CommodityVO();  //准备一个CommodityVO实例
            List<CommodityStandard> commodityStandardList = new ArrayList<>();  //准备商品规格集合实例
            commodityVO.setCommodityId(commodity.getCommodityId());
            commodityVO.setCommodityName(commodity.getCommodityName());
            commodityVO.setCommodityDescription(commodity.getCommodityDescription());
            commodityVO.setCommodityDiscount(commodity.getCommodityDiscount());
            commodityVO.setCommoditySales(commodity.getCommoditySales());
            commodityVO.setCommoditySaleMoney(commodity.getCommoditySaleMoney());
            commodityVO.setCommodityCategoryId(commodity.getCommodityCategoryId());
            commodityVO.setCommodityImgs(commodity.getCommodityImgs());
            commodityVO.setCreateTime(commodity.getCreateTime());
            commodityVO.setUpdateTime(commodity.getUpdateTime());
            //添加商品分类名称字段
            for(int i = 0; i < allCommodityCategoryList.size(); i++) {
                if(allCommodityCategoryList.get(i).getCategoryId() == commodity.getCommodityCategoryId()) {
                    commodityVO.setCommodityCategoryName(allCommodityCategoryList.get(i).getCategoryName());
                }
            }
            //添加商品规格集合字段
            for (int j = 0; j < allCommodityStandardList.size(); j++) {
                if(allCommodityStandardList.get(j).getCommodityId() == commodity.getCommodityId()){
                    commodityStandardList.add(allCommodityStandardList.get(j));
                }
            }
            commodityVO.setCommodityStandardList(commodityStandardList);
            //将数据添加到commodityVOList中
            commodityVOList.add(commodityVO);
        }

        //返回数据
        return new PageBean(commodityCount,commodityVOList);
    }

    /**
     * 查询所有的商品信息
     */
    @Override
    public PageBean getAllCommodityList() {
        //调用mapper层方法查询商品的总条数
        int commodityCount = commodityMapper.getCommodityCount();
        //调用mapper层方法查询所有商品信息
        List<Commodity> commodityList = commodityMapper.getAllCommodityList();

        /**此处要将商品列表的某些字段进行替换,并添加商品规格字段数据commodity_category_id --> categoryName(商品名称) 添加商品规格字段List<String> commodity_standard */
        //1.通过commodityList中字段commodity_category_id => commodityCategoryName
        //2.通过commodity_id 遍历商品规格表，将所有的规格信息封装在List<String>集合中
        List<CommodityCategory> allCommodityCategoryList = commodityCategoryMapper.getAllCommodityCategoryList();  //商品分类表所有数据
        List<CommodityStandard> allCommodityStandardList = commodityStandardMapper.getAllCommodityStandardList();  //商品规格表所有数据
        List<CommodityVO> commodityVOList = new ArrayList<>();
        for(Commodity commodity : commodityList) {
            CommodityVO commodityVO = new CommodityVO();  //准备一个CommodityVO实例
            List<CommodityStandard> commodityStandardList = new ArrayList<>();  //准备商品规格集合实例
            commodityVO.setCommodityId(commodity.getCommodityId());
            commodityVO.setCommodityName(commodity.getCommodityName());
            commodityVO.setCommodityDescription(commodity.getCommodityDescription());
            commodityVO.setCommodityDiscount(commodity.getCommodityDiscount());
            commodityVO.setCommoditySales(commodity.getCommoditySales());
            commodityVO.setCommoditySaleMoney(commodity.getCommoditySaleMoney());
            commodityVO.setCommodityCategoryId(commodity.getCommodityCategoryId());
            commodityVO.setCommodityImgs(commodity.getCommodityImgs());
            commodityVO.setCreateTime(commodity.getCreateTime());
            commodityVO.setUpdateTime(commodity.getUpdateTime());
            //添加商品分类名称字段
            for(int i = 0; i < allCommodityCategoryList.size(); i++) {
                if(allCommodityCategoryList.get(i).getCategoryId() == commodity.getCommodityCategoryId()) {
                    commodityVO.setCommodityCategoryName(allCommodityCategoryList.get(i).getCategoryName());
                }
            }
            //添加商品规格集合字段
            for (int j = 0; j < allCommodityStandardList.size(); j++) {
                if(allCommodityStandardList.get(j).getCommodityId() == commodity.getCommodityId()){
                    commodityStandardList.add(allCommodityStandardList.get(j));
                }
            }
            commodityVO.setCommodityStandardList(commodityStandardList);
            //将数据添加到commodityVOList中
            commodityVOList.add(commodityVO);
        }

        //返回数据
        return new PageBean(commodityCount,commodityVOList);
    }

    /**
     * 添加一条商品数据
     * @param commodity
     * @return
     */
    @Override
    public boolean saveCommodity(Commodity commodity) {
        try {
            commodityMapper.saveCommodity(commodity);
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    /**
     * 更新一条商品数据
     * @param commodity
     * @return
     */
    @Override
    public boolean updateCommodity(Commodity commodity) {
        try {
            commodityMapper.updateCommodity(commodity);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * 删除一条商品数据
     * @param commodity
     * @return
     */
    @Override
    public boolean deleteCommodity(int commodityId) {
        try {
            commodityMapper.deleteCommodity(commodityId);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * 根据商品分类对商品进行筛选
     * @param commodityCategoryId
     * @return
     */
    @Override
    public PageBean getCommodityListByCategory(int commodityCategoryId) {
        List<Commodity> commodityList = null;
        try {
            commodityList = commodityMapper.getCommodityListByCategory(commodityCategoryId);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageBean(0,null);
        }
        /**此处要将商品列表的某些字段进行替换,并添加商品规格字段数据commodity_category_id --> categoryName(商品名称) 添加商品规格字段List<String> commodity_standard */
        //1.通过commodityList中字段commodity_category_id => commodityCategoryName
        //2.通过commodity_id 遍历商品规格表，将所有的规格信息封装在List<String>集合中
        List<CommodityCategory> allCommodityCategoryList = commodityCategoryMapper.getAllCommodityCategoryList();  //商品分类表所有数据
        List<CommodityStandard> allCommodityStandardList = commodityStandardMapper.getAllCommodityStandardList();  //商品规格表所有数据
        List<CommodityVO> commodityVOList = new ArrayList<>();
        for(Commodity commodity : commodityList) {
            CommodityVO commodityVO = new CommodityVO();  //准备一个CommodityVO实例
            List<CommodityStandard> commodityStandardList = new ArrayList<>();  //准备商品规格集合实例
            commodityVO.setCommodityId(commodity.getCommodityId());
            commodityVO.setCommodityName(commodity.getCommodityName());
            commodityVO.setCommodityDescription(commodity.getCommodityDescription());
            commodityVO.setCommodityDiscount(commodity.getCommodityDiscount());
            commodityVO.setCommoditySales(commodity.getCommoditySales());
            commodityVO.setCommoditySaleMoney(commodity.getCommoditySaleMoney());
            commodityVO.setCommodityCategoryId(commodity.getCommodityCategoryId());
            commodityVO.setCommodityImgs(commodity.getCommodityImgs());
            commodityVO.setCreateTime(commodity.getCreateTime());
            commodityVO.setUpdateTime(commodity.getUpdateTime());
            //添加商品分类名称字段
            for(int i = 0; i < allCommodityCategoryList.size(); i++) {
                if(allCommodityCategoryList.get(i).getCategoryId() == commodity.getCommodityCategoryId()) {
                    commodityVO.setCommodityCategoryName(allCommodityCategoryList.get(i).getCategoryName());
                }
            }
            //添加商品规格集合字段
            for (int j = 0; j < allCommodityStandardList.size(); j++) {
                if(allCommodityStandardList.get(j).getCommodityId() == commodity.getCommodityId()){
                    commodityStandardList.add(allCommodityStandardList.get(j));
                }
            }
            commodityVO.setCommodityStandardList(commodityStandardList);
            //将数据添加到commodityVOList中
            commodityVOList.add(commodityVO);
        }

        //返回数据
        return new PageBean(commodityList.size(),commodityVOList);
    }

    /**
     * 批量删除商品 根据商品id commodityIds
     * @param commodityIds
     * @return
     */
    @Override
    public boolean deleteCommodityBatch(List<Integer> commodityIds) {
        try {
            int i = commodityMapper.deleteCommodityListBatch(commodityIds);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * 根据商品名称对商品进行模糊搜索 commodityName
     * @param commodityName
     * @return
     */
    @Override
    public PageBean getCommodityListByCommodityName(String commodityName) {
        List<Commodity> commodityList = null;
        try {
            commodityList = commodityMapper.getCommodityListByCommodityName(commodityName);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageBean(0,null);
        }
        /**此处要将商品列表的某些字段进行替换,并添加商品规格字段数据commodity_category_id --> categoryName(商品名称) 添加商品规格字段List<String> commodity_standard */
        //1.通过commodityList中字段commodity_category_id => commodityCategoryName
        //2.通过commodity_id 遍历商品规格表，将所有的规格信息封装在List<String>集合中
        List<CommodityCategory> allCommodityCategoryList = commodityCategoryMapper.getAllCommodityCategoryList();  //商品分类表所有数据
        List<CommodityStandard> allCommodityStandardList = commodityStandardMapper.getAllCommodityStandardList();  //商品规格表所有数据
        List<CommodityVO> commodityVOList = new ArrayList<>();
        for(Commodity commodity : commodityList) {
            CommodityVO commodityVO = new CommodityVO();  //准备一个CommodityVO实例
            List<CommodityStandard> commodityStandardList = new ArrayList<>();  //准备商品规格集合实例
            commodityVO.setCommodityId(commodity.getCommodityId());
            commodityVO.setCommodityName(commodity.getCommodityName());
            commodityVO.setCommodityDescription(commodity.getCommodityDescription());
            commodityVO.setCommodityDiscount(commodity.getCommodityDiscount());
            commodityVO.setCommoditySales(commodity.getCommoditySales());
            commodityVO.setCommoditySaleMoney(commodity.getCommoditySaleMoney());
            commodityVO.setCommodityCategoryId(commodity.getCommodityCategoryId());
            commodityVO.setCommodityImgs(commodity.getCommodityImgs());
            commodityVO.setCreateTime(commodity.getCreateTime());
            commodityVO.setUpdateTime(commodity.getUpdateTime());
            //添加商品分类名称字段
            for(int i = 0; i < allCommodityCategoryList.size(); i++) {
                if(allCommodityCategoryList.get(i).getCategoryId() == commodity.getCommodityCategoryId()) {
                    commodityVO.setCommodityCategoryName(allCommodityCategoryList.get(i).getCategoryName());
                }
            }
            //添加商品规格集合字段
            for (int j = 0; j < allCommodityStandardList.size(); j++) {
                if(allCommodityStandardList.get(j).getCommodityId() == commodity.getCommodityId()){
                    commodityStandardList.add(allCommodityStandardList.get(j));
                }
            }
            commodityVO.setCommodityStandardList(commodityStandardList);
            //将数据添加到commodityVOList中
            commodityVOList.add(commodityVO);
        }

        //返回数据
        return new PageBean(commodityList.size(), commodityVOList);
    }

    /**
     * 根据某个字段对商品进行排序输出 fieldName = commodity_sales || commodity_sale_money    sortRule = asc || desc
     * @param fieldName 字段名称
     * @param sortRule 排序规则
     * @return
     */
    @Override
    public PageBean getCommodityListSort(String fieldName, String sortRule) {
        List<Commodity> commodityList = null;
        try {
            commodityList = commodityMapper.getCommodityListSort(fieldName, sortRule);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageBean(0, null);
        }
        /**此处要将商品列表的某些字段进行替换,并添加商品规格字段数据commodity_category_id --> categoryName(商品名称) 添加商品规格字段List<String> commodity_standard */
        //1.通过commodityList中字段commodity_category_id => commodityCategoryName
        //2.通过commodity_id 遍历商品规格表，将所有的规格信息封装在List<String>集合中
        List<CommodityCategory> allCommodityCategoryList = commodityCategoryMapper.getAllCommodityCategoryList();  //商品分类表所有数据
        List<CommodityStandard> allCommodityStandardList = commodityStandardMapper.getAllCommodityStandardList();  //商品规格表所有数据
        List<CommodityVO> commodityVOList = new ArrayList<>();
        for(Commodity commodity : commodityList) {
            CommodityVO commodityVO = new CommodityVO();  //准备一个CommodityVO实例
            List<CommodityStandard> commodityStandardList = new ArrayList<>();  //准备商品规格集合实例
            commodityVO.setCommodityId(commodity.getCommodityId());
            commodityVO.setCommodityName(commodity.getCommodityName());
            commodityVO.setCommodityDescription(commodity.getCommodityDescription());
            commodityVO.setCommodityDiscount(commodity.getCommodityDiscount());
            commodityVO.setCommoditySales(commodity.getCommoditySales());
            commodityVO.setCommoditySaleMoney(commodity.getCommoditySaleMoney());
            commodityVO.setCommodityCategoryId(commodity.getCommodityCategoryId());
            commodityVO.setCommodityImgs(commodity.getCommodityImgs());
            commodityVO.setCreateTime(commodity.getCreateTime());
            commodityVO.setUpdateTime(commodity.getUpdateTime());
            //添加商品分类名称字段
            for(int i = 0; i < allCommodityCategoryList.size(); i++) {
                if(allCommodityCategoryList.get(i).getCategoryId() == commodity.getCommodityCategoryId()) {
                    commodityVO.setCommodityCategoryName(allCommodityCategoryList.get(i).getCategoryName());
                }
            }
            //添加商品规格集合字段
            for (int j = 0; j < allCommodityStandardList.size(); j++) {
                if(allCommodityStandardList.get(j).getCommodityId() == commodity.getCommodityId()){
                    commodityStandardList.add(allCommodityStandardList.get(j));
                }
            }
            commodityVO.setCommodityStandardList(commodityStandardList);
            //将数据添加到commodityVOList中
            commodityVOList.add(commodityVO);
        }

        //返回数据
        return new PageBean(commodityList.size(), commodityVOList);
    }
}
